package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.Enum.ResultEnum;
import com.dongdongshop.core.SpecificPrint;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TbItemCatServiceI;
import com.dongdongshop.service.TbTypeTemplateServiceI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedList;
import java.util.List;

@Controller
@RequestMapping("tbItemCatController")
public class TbItemCatController {

    @Reference
    private TbItemCatServiceI tbItemCatServiceI;

    private Logger logger= LoggerFactory.getLogger(TbBrandController.class);

    @Reference//模板
    private TbTypeTemplateServiceI tbTypeTemplateServiceI;

    //查询分类管理
    @RequestMapping("getFindAll")
    public String getFindAll(Model model){
        try {
            List<TbItemCat> tbItemCats = tbItemCatServiceI.getFindAll((long) 0);
            model.addAttribute("tbItemCats",tbItemCats);

            //模板
            List<TbTypeTemplate> tbTypeTemplates = tbTypeTemplateServiceI.getFiandsAll();
            model.addAttribute("tbTypeTemplates",tbTypeTemplates);

            logger.info(ResultEnum.SUCCESS.getMessage()+"，查询结果为：{}",tbItemCats);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage()+":{}",e);
        }
        return "admin/item_cat";
    }

    //查询下一级
    @RequestMapping("nextLevel")
    @ResponseBody
    public List<TbItemCat> nextLevel(Long pid){
        List<TbItemCat> tbItemCats = tbItemCatServiceI.nextLevel(pid);
        return tbItemCats;
    }

    //表头查询
    @RequestMapping("heads")
    @ResponseBody
    public List<TbItemCat> heads(Long id){
        try {
            //定义一个list集合
            List<TbItemCat> listTbItemCat = new LinkedList<>(); //LinkedList 有序存储
            //0 没有上级
            if(id != 0){
                //根据点击中pid当做id进行查询上一级
                TbItemCat tbItemCat = tbItemCatServiceI.getSelectById(id);
                listTbItemCat.add(tbItemCat);
                //pid等于0的等于顶级
                if(tbItemCat.getParentId() != 0){
                    TbItemCat tbItemCat1 = tbItemCatServiceI.getSelectById(tbItemCat.getParentId());
                    listTbItemCat.add(tbItemCat1);
                }
            }
            logger.info(ResultEnum.SUCCESS.getMessage()+"，查询结果为：{}",listTbItemCat);
            return listTbItemCat;
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage()+":{}",e);
        }

        return null;
    }

    //删除
    @RequestMapping("deleteTbItemCat")
    @ResponseBody
    public SpecificPrint deleteTbItemCat(String ids){
        try {
            String id=ids.replaceAll("'","");
            tbItemCatServiceI.deleteTbItemCat(id);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",id);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(id);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

    //增加
    @RequestMapping("saveTbItemCat")
    @ResponseBody
    public SpecificPrint saveTbItemCat(TbItemCat tbItemCat){
        try {
            tbItemCatServiceI.saveTbItemCat(tbItemCat);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",tbItemCat);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(tbItemCat);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

    //回显
    @RequestMapping("toUpdateTbItemCat")
    @ResponseBody
    public TbItemCat toUpdateTbItemCat(Long id){
        try {
            TbItemCat tbItemCat = tbItemCatServiceI.toUpdateTbItemCat(id);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，查询结果为：{}",tbItemCat);
            return tbItemCat;
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage()+":{}",e);
        }
        return null;
    }

    //确认修改
    @RequestMapping("updateTbItemCats")
    @ResponseBody
    public SpecificPrint updateTbItemCats(TbItemCat tbItemCat){
        try {
            tbItemCatServiceI.updateTbItemCats(tbItemCat);
            logger.info(ResultEnum.SUCCESS.getMessage()+"，参数为：{}",tbItemCat);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(tbItemCat);
        }catch (Exception e){
            logger.info(ResultEnum.ERROR.getMessage(),e);
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }
}
